package com.library.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.library.dao.BookTitle;
import com.library.dao.Librarian;
import com.library.dao.LibrarianDAO;
import com.library.dao.MySQLDAOFactory;
import com.library.dao.User;
import com.library.dao.UserDAO;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	private final static int LOGIN_USER_SUCCESS = 0;
	private final static int LOGIN_LIBRARIAN_SUCCESS = 1;
	private final static int LOGIN_ID_ERROR = 2;
	private final static int LOGIN_PW_ERROR = 3;

	private int userLogin(int id, String pw) {
		UserDAO userDao = (new MySQLDAOFactory()).getUserDAO();
		User user = userDao.findUser(id);
		if (user == null) {
			return LOGIN_ID_ERROR;
		} else if (user.getPassword().equals(pw)) {
			return LOGIN_USER_SUCCESS;
		} else
			return LOGIN_PW_ERROR;
	}

	private int librarianLogin(int id, String pw) {
		LibrarianDAO userDao = (new MySQLDAOFactory()).getLibrarianDAO();
		Librarian user = userDao.findLibrarian(id);
		if (user == null) {
			return LOGIN_ID_ERROR;
		} else if (user.getPassword().equals(pw)) {
			return LOGIN_LIBRARIAN_SUCCESS;
		} else
			return LOGIN_PW_ERROR;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try{
			String userId = request.getParameter("userid");
			String pasword = request.getParameter("password");

			int userNum = Integer.parseInt(userId);

			int stt = userLogin(userNum, pasword);
			if (stt == LOGIN_ID_ERROR) {
				stt = librarianLogin(userNum, pasword);
			}

			switch (stt) {
			case LOGIN_ID_ERROR: {
				String error = "Bạn nhập sai ID";
				request.setAttribute("error", error);
				String address = "loginerror.jsp";
				RequestDispatcher dispatcher = request
						.getRequestDispatcher(address);
				dispatcher.forward(request, response);
			}
				break;
			case LOGIN_PW_ERROR: {

				String error = "Bạn nhập sai Mật khẩu";
				request.setAttribute("error", error);
				String address = "loginerror.jsp";
				RequestDispatcher dispatcher = request
						.getRequestDispatcher(address);
				dispatcher.forward(request, response);
			}
				break;
			case LOGIN_USER_SUCCESS: {
				UserDAO userDAO = (new MySQLDAOFactory()).getUserDAO();
				ArrayList<BookTitle> borrowedBook = new ArrayList<BookTitle>();
				borrowedBook = userDAO
						.getBorrowedBook(userNum, Resource.CONTINUOUS);

				HttpSession session = request.getSession(true);
				session.setAttribute("instantUser", userDAO.findUser(userNum));

				// request.setAttribute("instantUser", user);
				request.setAttribute("borrowedBook", borrowedBook);
				String address = "user/userhome.jsp";
				RequestDispatcher dispatcher = request
						.getRequestDispatcher(address);
				dispatcher.forward(request, response);
			}
				break;
			case LOGIN_LIBRARIAN_SUCCESS: {
				HttpSession session = request.getSession(true);
				session.setAttribute("instantUser", (new MySQLDAOFactory())
						.getLibrarianDAO().findLibrarian(userNum));

				String address = "librarian/lending.jsp";
				RequestDispatcher dispatcher = request
						.getRequestDispatcher(address);
				dispatcher.forward(request, response);
			}
				break;
			}

		} catch(Exception e) {
			String error = "Bạn nhập không hợp lệ, vui lòng nhập lại!";
			request.setAttribute("error", error);
			String address = "loginerror.jsp";
			RequestDispatcher dispatcher = request
					.getRequestDispatcher(address);
			dispatcher.forward(request, response);
		}
	}
}
